package weather

import org.springframework.dao.DataIntegrityViolationException
import groovy.grape.Grape
@Grapes(@Grab(group='com.itextpdf', module='itextpdf', version='5.1.2'))
import java.io.FileOutputStream
import java.io.IOException
import com.itextpdf.text.Document
import com.itextpdf.text.DocumentException
import com.itextpdf.text.Paragraph
import com.itextpdf.text.pdf.PdfWriter


class OrdersController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }
    
    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [ordersInstanceList: Orders.list(params), ordersInstanceTotal: Orders.count()]
    }

    def create() {
        [ordersInstance: new Orders(params)]
    }
    
    def calc(Float bf, Float time){
        if (bf == 0)
            return time*0        
        else
        if (bf < 101)
            return time*1
        else
        if (bf < 201)
            return time*2
        else
        if (bf < 301)
            return time*3
        else
        if (bf < 401)
            return time*4   
        else
        if (bf < 501)
            return time*5
        else
        if (bf < 1001)
            return time*5.5
                
    }
    
    
    def save() {
        def ordersInstance = new Orders(params)
        def Float t = 0
        ordersInstance.timeCherry = calc(ordersInstance.bfCherry, 0.5)
        if (ordersInstance.timeCherry > t) {t=ordersInstance.timeCherry }
        ordersInstance.timeCurly  = calc(ordersInstance.bfCurly, 1.5)
        if (ordersInstance.timeCurly > t) {t=ordersInstance.timeCurly }
        ordersInstance.timeGenuine = calc(ordersInstance.bfGenuine, 3.0)
        if (ordersInstance.timeGenuine > t) {t=ordersInstance.timeGenuine }        
        ordersInstance.timeWenge  = calc(ordersInstance.bfWenge, 5.0)
        if (ordersInstance.timeWenge > t) {t=ordersInstance.timeWenge }        
        ordersInstance.timeWhite  = calc(ordersInstance.bfWhite, 2.3)
        if (ordersInstance.timeWhite > t) {t=ordersInstance.timeWhite }        
        ordersInstance.timeSawdust = calc(ordersInstance.bfSawdust , 0)
        if (ordersInstance.timeSawdust > t) {t=ordersInstance.timeSawdust }
        ordersInstance.time = t
        
        if (t==0){
            flash.message = "You need to buy something!"
            render(view: "create", model: [ordersInstance: ordersInstance])
            return

        }
        
        ordersInstance.priceCherry = ordersInstance.bfCherry* 5.95
        ordersInstance.priceCurly  = ordersInstance.bfCurly* 6.0
        ordersInstance.priceGenuine = ordersInstance.bfGenuine* 9.6
        ordersInstance.priceWenge  = ordersInstance.bfWenge* 22.35
        ordersInstance.priceWhite  = ordersInstance.bfWhite* 6.70
        ordersInstance.priceSawdust = ordersInstance.bfSawdust*1.5
        
        ordersInstance.total = ordersInstance.priceCherry + 
                               ordersInstance.priceCurly + 
                               ordersInstance.priceGenuine + 
                               ordersInstance.priceWenge + 
                               ordersInstance.priceWhite  + 
                               ordersInstance.priceSawdust
            
        if (!ordersInstance.save(flush: true)) {
            render(view: "create", model: [ordersInstance: ordersInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'orders.label', default: 'Orders'), ordersInstance.id])
        redirect(action: "show", id: ordersInstance.id)
    }

    def show(Long id) {
        def ordersInstance = Orders.get(id)
        if (!ordersInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'orders.label', default: 'Orders'), id])
            redirect(action: "list")
            return
        }

        [ordersInstance: ordersInstance]
    }

    def edit(Long id) {
        def ordersInstance = Orders.get(id)
        if (!ordersInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'orders.label', default: 'Orders'), id])
            redirect(action: "list")
            return
        }

        [ordersInstance: ordersInstance]
    }

    def update(Long id, Long version) {
        def ordersInstance = Orders.get(id)
        if (!ordersInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'orders.label', default: 'Orders'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (ordersInstance.version > version) {
                ordersInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'orders.label', default: 'Orders')] as Object[],
                          "Another user has updated this Orders while you were editing")
                render(view: "edit", model: [ordersInstance: ordersInstance])
                return
            }
        }

        ordersInstance.properties = params

        if (!ordersInstance.save(flush: true)) {
            render(view: "edit", model: [ordersInstance: ordersInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'orders.label', default: 'Orders'), ordersInstance.id])
        redirect(action: "show", id: ordersInstance.id)
    }

    def delete(Long id) {
        def ordersInstance = Orders.get(id)
        if (!ordersInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'orders.label', default: 'Orders'), id])
            redirect(action: "list")
            return
        }

        try {
            ordersInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'orders.label', default: 'Orders'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'orders.label', default: 'Orders'), id])
            redirect(action: "show", id: id)
        }
    }
    
    def generatePDF(Long id){
        def ordersInstance = Orders.get(id)
        if (!ordersInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'orders.label', default: 'Orders'), id])
            redirect(action: "list")
            return
        }

        [ordersInstance: ordersInstance]
    }
}
